//
//  UIView+Extension.swift
//  DanTangLKM
//
//  Created by LKM on 16/8/22.
//  Copyright © 2016年 LKM. All rights reserved.
//

import UIKit

extension UIView {

    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
        // Drawing code
    }
    */
    
    /// 裁剪 view 的圆角
    func clipRectCorner(_ direction: UIRectCorner, cornerRadius: CGFloat) {
        let cornerSize = CGSize(width: cornerRadius, height: cornerRadius)
        let maskPath = UIBezierPath(roundedRect: bounds, byRoundingCorners: direction, cornerRadii: cornerSize)
        let maskLayer = CAShapeLayer()
        maskLayer.frame = bounds
        maskLayer.path = maskPath.cgPath
        layer.addSublayer(maskLayer)
        layer.mask = maskLayer
    }
    
    /// x
    var x: CGFloat {
        get {
            return frame.origin.x
        }
        set(newValue) {
            var tempFrame: CGRect = frame
            tempFrame.origin.x    = newValue
            frame                 = tempFrame
        }
    }
    
    /// y
    var y: CGFloat {
        get {
            return frame.origin.y
        }
        set(newValue) {
            var tempFrame: CGRect = frame
            tempFrame.origin.y    = newValue
            frame                 = tempFrame
        }
    }
    
    /// height
    var height: CGFloat {
        get {
            return frame.size.height
        }
        set(newValue) {
            var tempFrame: CGRect = frame
            tempFrame.size.height = newValue
            frame                 = tempFrame
        }
    }
    
    /// width
    var width: CGFloat {
        get {
            return frame.size.width
        }
        set(newValue) {
            var tempFrame: CGRect = frame
            tempFrame.size.width = newValue
            frame = tempFrame
        }
    }
    
    /// size
    var size: CGSize {
        get {
            return frame.size
        }
        set(newValue) {
            var tempFrame: CGRect = frame
            tempFrame.size = newValue
            frame = tempFrame
        }
    }
    
    /// centerX
    var centerX: CGFloat {
        get {
            return center.x
        }
        set(newValue) {
            var tempCenter: CGPoint = center
            tempCenter.x = newValue
            center = tempCenter
        }
    }
    
    /// centerY
    var centerY: CGFloat {
        get {
            return center.y
        }
        set(newValue) {
            var tempCenter: CGPoint = center
            tempCenter.y = newValue
            center = tempCenter;
        }
    }
    
    
    public func findViewWithTag(tag:Int)->UIView?{
        return self.viewWithTag(tag);
    }
    
    private struct ConstKeys {
        static var showActivityCount        = "showActivityCount"
    }
    
    
    /**
     
     调用多少次 就得调用多少次hideProgressActivity();
     */
    public func showProgressActivity(tips:String?=""){
        
        var count = 0;
        if let showActivityCount = getTag(&ConstKeys.showActivityCount) {
            count=showActivityCount as! Int;
        }
        count+=1;
        
        setTag(&ConstKeys.showActivityCount, count);
        
        makeToastActivity(.center);
    }
    
    public func hideProgressActivity(){
        var count = 0;
        if let showActivityCount = getTag(&ConstKeys.showActivityCount) {
            count=showActivityCount as! Int;
        }
        count-=1;
        
        count = max(count, 0);
        
        setTag(&ConstKeys.showActivityCount, count);
        if(count<=0){
            hideToastActivity();
        }
        
        
    }

}
